<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>点击复制文本</title>
    <style type="text/css">
      .wrapper {
        position: relative;
      }
      #input {
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
        z-index: -10;
      }
    </style>
  </head>
  <body>
    <div class="wrapper">
      <p id="text">我把你当兄弟你却想着复制我？</p>
      <!-- <textarea id="input">这是幕后黑手</textarea> -->
      <button onclick="copyText()">copy</button>
    </div>

    <script type="text/javascript">
      function copyText() {
        // var text = document.getElementById("text").innerText;
        // var input = document.getElementById("input")
        // var input = document.createElement("textarea");
        // input.value = text; // 修改文本框的内容
        // input.select(); // 选中文本
        var text = document.getElementById("text");
        var range = document.createRange();

        range.selectNode(text);
        window.getSelection().addRange(range);
        document.execCommand("copy"); // 执行浏览器复制命令
        alert("复制成功");
      }
    </script>
  </body>
</html>
